#include<cstdio>
#include<unistd.h>
#include"RingBuffer.hpp"

using namespace RingBufferModule;

void* Consumer(void* asg)
{
    RingBuffer<int>* rb = static_cast<RingBuffer<int>*>(asg);
    while(true)
    {
        int data;

        sleep(1);
        rb->Pop(data);

        printf("Consumer, 消费了一个数据:%d\n", data);
    }
    return nullptr;
}

void* Producer(void* asg)
{
    RingBuffer<int>* rb = static_cast<RingBuffer<int>*>(asg);
    int data = 10;
    while(true)
    {
        rb->Equeue(data);
        printf("Producer, 生产了一个数据:%d\n", data);

        //sleep(1);
        data++;
    }
    return nullptr;
}

int main()
{
    RingBuffer<int>* rb = new RingBuffer<int>(5);

    pthread_t c1,p1, c2, p2, p3;
    pthread_create(&c1, nullptr, Consumer, (void*)rb);
    pthread_create(&c2, nullptr, Consumer, (void*)rb);
    pthread_create(&p1, nullptr, Producer, (void*)rb);
    pthread_create(&p2, nullptr, Producer, (void*)rb);
    pthread_create(&p3, nullptr, Producer, (void*)rb);

    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(p3,nullptr);
    return 0;
}